package automatic.generate.code.model;

import java.io.File;
import java.util.HashMap;

import org.misalen.utils.TextUtil;

public class FilePath extends HashMap<String, String> {

	private String proName;
	private String parentPath;
	private String modelName;
	private String simpleName;
	private boolean sysModel;

	public FilePath(String proName, String parentPath, String modelName, String simpleName, boolean sysModel) {
		super();
		this.proName = proName;
		this.parentPath = parentPath;
		this.modelName = modelName;
		this.simpleName = simpleName;
		this.sysModel = sysModel;
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void bulidPathAndName() {
		if (size() > 0) {
			clear();
		}
		put(PathKey.MODEL_NAME.name(),
				String.format(modelName, proName.replaceAll("-", "."), sysModel ? "sys" : "pro", simpleName));
		StringBuffer adminPath = new StringBuffer();
		adminPath.append(parentPath);
		adminPath.append("/");
		adminPath.append(proName);
		adminPath.append("-admin/src/main/");
		String adminPathString = adminPath.toString();

		StringBuffer adminControllersPath = new StringBuffer();
		adminControllersPath.append(adminPathString);
		adminControllersPath.append("java/org/");
		adminControllersPath.append(proName.replaceAll("-", "/"));
		adminControllersPath.append("/admin/controllers/");
		adminControllersPath.append(simpleName);
		adminControllersPath.append("Controller.java");

		put(PathKey.ADMIN_CONTROLLERS.name(), adminControllersPath.toString());

		StringBuffer adminViewPath = new StringBuffer();
		adminViewPath.append(adminPathString);
		adminViewPath.append("webapp/view/");
		adminViewPath.append(TextUtil.toLowerCaseFirstOne(simpleName));
		adminViewPath.append("/");
		String adminViewPathString = adminViewPath.toString();
		File file = new File(adminViewPathString);
		if (!file.exists()) {
			file.mkdirs();
		}
		put(PathKey.ADMIN_VIEW_LIST.name(), adminViewPathString + "list.jsp");
		put(PathKey.ADMIN_VIEW_ADD.name(), adminViewPathString + "add.jsp");
		put(PathKey.ADMIN_VIEW_UPDATE.name(), adminViewPathString + "update.jsp");
		put(PathKey.ADMIN_VIEW_DETAILS.name(), adminViewPathString + "details.jsp");

		StringBuffer dataBasePath = new StringBuffer();
		dataBasePath.append(parentPath);
		dataBasePath.append("/");
		dataBasePath.append(proName);
		dataBasePath.append("-database/src/main/");
		String dataBasePathString = dataBasePath.toString();

		StringBuffer dataBaseRepositoryPath = new StringBuffer();
		dataBaseRepositoryPath.append(dataBasePathString);
		dataBaseRepositoryPath.append("java/org/");
		dataBaseRepositoryPath.append(proName.replaceAll("-", "/"));
		dataBaseRepositoryPath.append("/database/repository/mysql/");
		dataBaseRepositoryPath.append(simpleName);
		dataBaseRepositoryPath.append("Repository.java");
		put(PathKey.DATABASE_REPOSITORY.name(), dataBaseRepositoryPath.toString());

	}

	public String get(PathKey key) {
		return super.get(key.name());
	}

	public enum PathKey {
		MODEL_NAME, //
		ADMIN_CONTROLLERS, //
		ADMIN_VIEW_LIST, //
		ADMIN_VIEW_ADD, //
		ADMIN_VIEW_UPDATE, //
		ADMIN_VIEW_DETAILS, //
		DATABASE_REPOSITORY
	}
}
